package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

public class khxxUtil {
	
	static List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
	static List<String> kh_list=new ArrayList<String>();
	private static String filename = "khxx.db";
	 /**
	  * 初始化加载客户数据
	  * */
	static{
		readDataByDBFile();
	}
	
	
	
	/***
	 * 从KHXX.DB文件中读取客户列表信息
	 * @throws IOException 
	 * 
	 * **/
	public static void readDataByDBFile() {
	    /**File file =new File("./khxx.db");
	    if(!file.exists()){
	    	System.out.println("文件不存在");
	    	return;
	    	
	    }
	    if(file.length()==0){
	    	System.out.println("文件内容为空");
	    	return;
	    	
	    }**/
		try {
		InputStream inputStream=new FileInputStream(new File(filename));
		if(inputStream.available() == 0){
			System.out.println("文件内容为空");
	    	return;
		}
		String data="";
	    String[] field={"GFMC","AREA","LXR","NSRSBH","BZ","DZDH","BANK"};
		@SuppressWarnings("resource")
		BufferedReader fr=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
		
			while((data=fr.readLine()) != null){
				String[] arr=data.split("\t");
				kh_list.add(arr[0]);
				HashMap<String,String> tmp=new HashMap<String,String>();
				for(int i=0;i<field.length;i++){
					if(i<arr.length){
						tmp.put(field[i], arr[i]);
					}else{
						tmp.put(field[i],"");
					}
				}

			    list.add(tmp);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/***
	 *返回客户列表信息
	 * 
	 */
	public static List<HashMap<String,String>> getMsg(){
		 List<HashMap<String,String>> showlist=new ArrayList<HashMap<String,String>>();
		 for(int i=0;i<list.size();i++){
			 HashMap<String,String> tmp=list.get(i);
			 tmp.put("NO",String.valueOf(i+1));
			 showlist.add(tmp);
		 }
		 
		 return showlist;
	}
	
	
	/***
	 * 判断是否已存在该客户信息
	 * 
	 * **/	
	public static boolean Find(String gfmc){
	
		boolean flag=false;
		for(int i=0;i<kh_list.size();i++){
	
			if(kh_list.get(i).equals(gfmc)&&!kh_list.get(i).equals("")){
				flag=true;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 查找位置
	 * @param gfmc
	 * @return
	 */
	public static Integer FindPosition(String gfmc){
		
		for(int i=0;i<kh_list.size();i++){
	
			if(kh_list.get(i).equals(gfmc)&&!kh_list.get(i).equals("")){
				return i;
			}
		}
		return -1;
	}
	
	/***
	 * 检查数据合法性
	 * 
	 ***/	
	public static boolean checkMsg(HashMap<String,String> msg){
		if(msg.get("GFMC").equals("")||msg.get("NSRSBH").equals("")){
		return false;
		}
		return true;
	}
	
	/***
	 * 新增数据到客户信息表
	 * 
	 ***/	
	public static JSONObject addMsg(HashMap<String,String> msg){
		JSONObject result=new JSONObject();
		if(checkMsg(msg)){
		   if(!Find(msg.get("GFMC"))){	   
			   list.add(msg);
			   writeFile();
			   result.put("state", true);
			
		   }else{
			   result.put("error", "企业名称不能重复");
			   result.put("state", false);
		   }	
		}else{
			  result.put("error", "企业名称或税号不能为空.");
			   result.put("state", false);
		}
		return result;
	}
	
	
	/***
	 * 根据数组下标更新信息
	 * 
	 * **/	
	public static JSONObject updateMsg(int index,HashMap<String,String> msg){
		JSONObject result=new JSONObject();
		if(checkMsg(msg)){
		   if(!Find(msg.get("GFMC"))){	   
				list.get(index).putAll(msg);
				writeFile();
				result.put("state", true);
			
		   }else{
			   if(list.get(index).get("GFMC").equals(msg.get("GFMC"))){
				   list.get(index).putAll(msg);
				   writeFile();
				   result.put("state", true);
			   }else{
				  result.put("error", "企业名称不能重复");
				  result.put("state", false);
			   }
		   }	
		}else{
			  result.put("error", "企业名称或税号不能为空.");
			   result.put("state", false);
		}
		return result;
	
	}
	
	
	/***
	 * 重写数据至文件
	 * 
	 * **/	
	public static void writeFile(){
		try {
		File file=new File(filename);
		String content="";
		for(int i=0;i<list.size();i++){
			  content+=list.get(i).get("GFMC")+"\t"+
				      list.get(i).get("AREA")+"\t"+
				      list.get(i).get("LXR")+"\t"+
				      list.get(i).get("NSRSBH")+"\t"+
				      list.get(i).get("BZ")+"\t"+
				      list.get(i).get("DZDH")+"\t"+
				      list.get(i).get("BANK")+"\r\n";
		}
		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
		out.write(content.toCharArray());  
        out.flush();  
        out.close(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
			
	}
	
	
	/**
	 * 
	 * 根据客户名称模糊搜索结果
	 * 
	 * */
	public static List<HashMap<String,String>> Search(String fieldName,String SearchValue){
		List<HashMap<String,String>> tmp=new ArrayList<HashMap<String,String>>();
		//List<Integer> indexArray=new ArrayList<Integer>() ;
		for(int i=0;i<list.size();i++){
			HashMap<String,String> tmp2=list.get(i);
			if(tmp2.get(fieldName).indexOf(SearchValue)!=-1){
				tmp2.put("NO", String.valueOf(i+1));
				tmp.add(tmp2);
			}
		}
		return tmp;
	}
	
	
	/***
	 * 根据数组下标删除信息
	 * 
	 * **/	
	public static void delMsg(int index){
		
		list.remove(index);
		writeFile();
		//list = new ArrayList<HashMap<String,String>>();
		//readDataByDBFile();
		//getMsg();
	}
	
	/**
	 * 根据购方名称修改税号
	 * @param name
	 * @param msg
	 * @return 
	 */
	public static JSONObject updateMsg(String name, HashMap<String, String> msg) {
		JSONObject result=new JSONObject();
		if(checkMsg(msg)){
			Integer position = FindPosition(name);
			if(position==-1){
				list.add(msg);
				writeFile();
				result.put("state", true);
			}else{
				list.get(position).putAll(msg);
				writeFile();
				result.put("state", true);
			}
		}else{
			result.put("error", "企业名称不能重复");
			result.put("state", false);
		}
		return result;
	}
	
	

	
//	public static void main(String[] args) {
//		khxx a=new khxx();
//		
//		HashMap<String,String> msg=new 	HashMap<String,String>();
//		msg.put("AREA", "测试");
//		msg.put("GFMC", "测试5");
//		msg.put("LXR", "测试");
//		msg.put("BZ", "测试");
//		msg.put("NSRSBH", "测试");
//		msg.put("DZDH", "fsdfdsfdf");
//		msg.put("BANK", "2045254245245245");
//
//		//if((boolean) a.addMsg(msg).get("state"))System.out.println("success");else System.out.println("fail");
//		//a.updateMsg(0, msg);
//		//System.out.println(a.Search("AREA", "江门")); 
//		System.out.println(a.getMsg()); 
//	}
	
}
